﻿using System;
using UnityEngine;

namespace Framework
{
    public static class GameObjectUtil
    {
        public static void SetLayer(GameObject gameObject, int layer, bool recursive)
        {
            gameObject.layer = layer;
            if (recursive)
            {
                Transform transform = gameObject.transform;
                for (int i = transform.childCount - 1; i >= 0; --i)
                {
                    Transform childTransform = transform.GetChild(i);
                    SetLayer(childTransform.gameObject, layer, true);
                }
            }
        }

        public static void AddComponent(GameObject gameObject, Type type)
        {
            if (gameObject.GetComponent(type) == null)
            {
                gameObject.AddComponent(type);
            }
        }
    }
}